package it.megasoft78.turingmachine;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class EditTupleActivity extends Activity implements OnClickListener {
	private Spinner ddlCurrentState;
	private Spinner ddlScannedSymbol;
	private Spinner ddlFinalState;
	private Spinner ddlPrintSymbol;
	private Spinner ddlMoveTape;
	private ArrayList<TupleEntity> tuples;
	private int selectedIndex;
	private ArrayList<String> states;
	private ArrayList<String> symbols;
	private ArrayList<String> moveTypes;
	private int mode;
	public static final int ADD_TUPLE = 1;
	public static final int EDIT_TUPLE = 2;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_tuple);

		Bundle extras = getIntent().getExtras();

		if (extras != null) {
			mode = extras.getInt("Mode");
			tuples = (ArrayList<TupleEntity>) extras.getSerializable("Tuples");
			selectedIndex = extras.getInt("SelectedIndex");
			states = this.getStates();
			symbols = this.getSymbols();
			moveTypes = new ArrayList<String>(Arrays.asList(getResources()
					.getStringArray(R.array.arrMoveTypes)));
		}

		ddlCurrentState = (Spinner) findViewById(R.id.ddlCurrentState);
		ddlScannedSymbol = (Spinner) findViewById(R.id.ddlScannedSymbol);
		ddlFinalState = (Spinner) findViewById(R.id.ddlFinalState);
		ddlPrintSymbol = (Spinner) findViewById(R.id.ddlPrintSymbol);
		ddlMoveTape = (Spinner) findViewById(R.id.ddlMoveTape);

		Button btnNewCurrentState = (Button) findViewById(R.id.btnNewCurrentState);
		Button btnNewScannedSymbol = (Button) findViewById(R.id.btnNewScannedSymbol);
		Button btnNewFinalState = (Button) findViewById(R.id.btnNewFinalState);
		Button btnNewPrintSymbol = (Button) findViewById(R.id.btnNewPrintSymbol);
		Button btnNewMoveTape = (Button) findViewById(R.id.btnNewMoveTape);

		Button btnOk = (Button) findViewById(R.id.btnOk);
		Button btnCancel = (Button) findViewById(R.id.btnCancel);

		btnNewCurrentState.setOnClickListener(this);
		btnNewScannedSymbol.setOnClickListener(this);
		btnNewFinalState.setOnClickListener(this);
		btnNewPrintSymbol.setOnClickListener(this);
		btnNewMoveTape.setOnClickListener(this);

		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		ArrayAdapter<String> adapterMoveTypes = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, moveTypes);
		adapterMoveTypes
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ddlMoveTape.setAdapter(adapterMoveTypes);
		ddlMoveTape.setPromptId(R.string.msgSelectMoveType);

		ArrayAdapter<String> adapterStates = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, states);
		adapterStates
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ddlCurrentState.setAdapter(adapterStates);
		ddlFinalState.setAdapter(adapterStates);
		ddlCurrentState.setPromptId(R.string.msgSelectState);
		ddlFinalState.setPromptId(R.string.msgSelectState);

		ArrayAdapter<String> adapterSymbols = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, symbols);
		adapterSymbols
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ddlScannedSymbol.setAdapter(adapterSymbols);
		ddlPrintSymbol.setAdapter(adapterSymbols);

		ddlScannedSymbol.setPromptId(R.string.msgSelectSymbol);
		ddlPrintSymbol.setPromptId(R.string.msgSelectSymbol);

		if (mode == EditTupleActivity.EDIT_TUPLE) {
			TupleEntity selectedTuple = tuples.get(selectedIndex);
			
			ddlCurrentState.setSelection(states.indexOf(selectedTuple
					.getCurrentState()));
			ddlScannedSymbol.setSelection(symbols.indexOf(selectedTuple
					.getScannedSymbol()));
			ddlFinalState.setSelection(states.indexOf(selectedTuple
					.getFinalState()));
			ddlPrintSymbol.setSelection(symbols.indexOf(selectedTuple
					.getPrintSymbol()));
			ddlMoveTape.setSelection(selectedTuple.getMoveTape());
		}
	}

	private ArrayList<String> getStates() {
		ArrayList<String> states = new ArrayList<String>();

		for (int t = 0; t < tuples.size(); t++) {
			TupleEntity tuple = tuples.get(t);

			if (!states.contains(tuple.getCurrentState()))
				states.add(tuple.getCurrentState());

			if (!states.contains(tuple.getFinalState()))
				states.add(tuple.getFinalState());
		}

		Collections.sort(states);

		return states;
	}

	private ArrayList<String> getSymbols() {
		ArrayList<String> symbols = new ArrayList<String>();

		for (int t = 0; t < tuples.size(); t++) {
			TupleEntity tuple = tuples.get(t);

			if (!symbols.contains(tuple.getScannedSymbol()))
				symbols.add(tuple.getScannedSymbol());

			if (!symbols.contains(tuple.getPrintSymbol()))
				symbols.add(tuple.getPrintSymbol());
		}

		Collections.sort(symbols);

		return symbols;
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btnNewCurrentState:
			break;

		case R.id.btnNewScannedSymbol:
			break;

		case R.id.btnNewFinalState:
			break;

		case R.id.btnNewPrintSymbol:
			break;

		case R.id.btnNewMoveTape:
			break;

		case R.id.btnOk:
			TupleEntity tuple = new TupleEntity();
			tuple.setCurrentState(states.get(ddlCurrentState
					.getSelectedItemPosition()));
			tuple.setFinalState(states.get(ddlFinalState
					.getSelectedItemPosition()));
			tuple.setScannedSymbol(symbols.get(ddlScannedSymbol
					.getSelectedItemPosition()));
			tuple.setPrintSymbol(symbols.get(ddlPrintSymbol
					.getSelectedItemPosition()));
			tuple.setMoveTape(ddlMoveTape.getSelectedItemPosition());

			Intent resultIntent = new Intent();
			resultIntent.putExtra("SelectedIndex", selectedIndex);
			resultIntent.putExtra("ChangedTuple", tuple);
			setResult(Activity.RESULT_OK, resultIntent);
			this.finish();
			break;

		case R.id.btnCancel:
			this.finish();
			break;
		}
	}
}
